/**
 * Created by xie on 2018/4/4.
 */
angular.module('module.auth')
    .controller('RegisterController', RegisterController);

RegisterController.$inject = ['$scope', '$state', '$cookieStore', '$rootScope', 'AutherService'];

// 登录
function RegisterController($scope, $state, $cookieStore, $rootScope, AutherService) {
    var vm = this;
    vm.btnMessage = "发送验证码";
    vm.sentMessage = sentMessage;
    vm.btnclass = "btnclass";
    vm.addUser = addUser;




    vm.user = {
        "userid": null,
        "password": "",
        "nickname": "请修改昵称",
        "usertype": 1,
        "photo": "http://sfs.shuwang.info/sfcommon/1030/2018-04-12/6yc6vZwtL-linghu.jpg",
        "createtime": "",
        "usercollect": "幽静唯美,花中萌物,奇花异草",
        "sex": "男",
        "authcode": null
    }

    function sentMessage() {
        vm.btnMessage = "已发送验证码";
        vm.btnclass = "btnclasschange";
        AutherService.sentMessage(vm.user.userid).$promise.then(function (data) {
            response = data;
            console.log(response);
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });
    }

    function addUser() {

        if (!vm.user.userid) {
            vm.user = null;
            return alert("请输入手机号");
        }
        if (!vm.user.authcode) {
            vm.user = null;
            return alert("请输入验证码");
        }
        if(vm.user.password.length<6)
        {
            vm.userinfo = null;
            return alert("密码需大于6位");
        }
        if (!vm.user.password&&!vm.user.pwd) {
            vm.userinfo = null;
            return alert("密码不能为空");

        }
        if (vm.user.password != vm.user.pwd) {
            vm.user = null;
            return alert("两次输入的密码不一致");
        }
        vm.user.nickname=vm.user.userid;
        AutherService.addUser(vm.user).$promise.then(function (data) {
            request = data;
            console.log(request)
            alert("注册成功");
            $state.go('login');
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });

    }


}
